package com.chen.learnknowledge.controller;

import com.chen.learnknowledge.entity.Employee;
import com.chen.learnknowledge.service.EmployeeService;
import com.chen.learnknowledge.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    //新增一条数据emp
    @PostMapping ("/addEmpOne")
    public R addEmpOne(@RequestBody Employee employee){
        System.out.println(employee);
        Long count = employeeService.addEmpOne(employee);
        if (StringUtils.isEmpty(employee)){
            return R.error();
        }
        return R.ok();
    }

    //批量新增emp
    @PostMapping ("/addEmps")
    public R addEmps(@RequestBody List<Employee> employees){
        employeeService.addEmps(employees);
        return R.ok();
    }

    /**
     * 模糊分页查询
     * @param pageSize 分页数据量
     * @param pageNum 第几页
     * @param name 根据LastName做模糊查询
     * @return
     */
    @GetMapping("/getPageList")
    public R getPageList(@RequestParam("pageSize") Integer pageSize,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("name") String name){
        List<Employee> employeeList = employeeService.getPageList(pageSize,pageNum,name);
        return R.ok().put("employeeList",employeeList);

    }

    //根据用户id查询
    @GetMapping("/getEmpById")
    public R getEmpById(@RequestParam("id") Integer id){
       Employee employee = employeeService.getEmpById(id);
       if (StringUtils.isEmpty(employee)){
           return R.error();
       }
       return R.ok().put("emp",employee);
    }

    //修改emp
    @PutMapping("/updateEmp")
    public R updateEmp(@RequestBody Employee employee){
        Boolean isUpdate = employeeService.updateEmp(employee);
        if (!isUpdate){
            return R.error();
        }
        return R.ok();
    }
    //根据id删除emp
    @DeleteMapping("/deleteEmpById")
    public R deleteEmpById(@RequestParam("id") Integer id){
        Integer isDelete = employeeService.deleteEmpById(id);
        if (isDelete==0){
            return R.error();
        }
        return R.ok();
    }

}
